package com.epalmpay.controller.apiweixin;

import com.epalmpay.commom.BaseController;
import com.epalmpay.entity.ReservationRule;
import com.epalmpay.entity.Response;
import com.epalmpay.service.weixin.IReservationConcessionsRuleService;
import com.epalmpay.service.weixin.IReservationRuleService;
import com.epalmpay.service.weixin.IReservationTimeRuleService;
import com.epalmpay.util.DateUtil;
import com.epalmpay.util.Tools;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;

/**
 * Created by Administrator on 2018/6/5.
 * @author wkf  ReservationRuleController.java 预约规则
 */
@Controller
@RequestMapping(value = "/api/ReservationRuleMenu")
public class ReservationRuleController extends BaseController {

    @Autowired
    protected IReservationRuleService reservationRuleService;//预约规则

    @Autowired
    protected IReservationTimeRuleService reservationTimeRuleService;//预约规则》时间

    @Autowired
    protected IReservationConcessionsRuleService reservationConcessionsRuleService;//预约规则》满减优惠



    /**
     * 根据渠道ID，门店ID，检查该门店是否存在预约规则
     * @param groupId
     * @param shopId
     * @return
     */
    @RequestMapping(value="/checkShopReservation",method = {RequestMethod.GET,RequestMethod.POST})
    @ResponseBody
    public Response checkShopReservation(@RequestParam Long groupId, @RequestParam Long shopId){
        logBefore(logger, "检查指定门店是否存在预约规则和当前时间是否可以预约");
        if(groupId == null || shopId == null){
            return Response.getError("请求参数不完整");
        }else{
            try {
                return Response.getSuccess(reservationRuleService.checkShopReservation(groupId,shopId));
            } catch (Exception e) {
                e.printStackTrace();
                return Response.getError(e.getMessage());
            }
        }
    }


    @RequestMapping(value="/checkReservationTimeCanOrNot",method = {RequestMethod.GET,RequestMethod.POST})
    @ResponseBody
    public Response checkReservationTimeCanOrNot(@RequestParam Long groupId,@RequestParam Long shopId,
                                                 @RequestParam Long memberId,
                                                 @RequestParam Long reservationRuleId,
                                                 @RequestParam Integer personCount,
                                                 @RequestParam String reservationyyyyMMdd,
                                                 @RequestParam String reservationHHmm,
                                                 @RequestParam String message,
                                                 @RequestParam Integer reservationType,
                                                 @RequestParam Long timeRuleId){
        logBefore(logger, "判断选择的时间段是否约满");
        if(groupId == null || shopId == null || reservationRuleId == null || personCount == null || Tools.isEmpty(reservationyyyyMMdd) || reservationType == null){
            return Response.getError("请求参数不完整");
        }
        if(reservationType.equals(0) && Tools.isEmpty(reservationHHmm)){
            return Response.getError("全天类预约不可缺失预约时间");
        }
        //在预约类型为1，即按时段预约时，时段ID不可为空
        if(reservationType.equals(1) && timeRuleId == null){
            return Response.getError("按时段预约时不可缺失时间段ID");
        }
        try {
            return Response.getSuccess(reservationRuleService.checkReservationTimeCanOrNot(groupId,shopId,memberId,reservationRuleId,personCount,reservationyyyyMMdd,reservationHHmm,message,reservationType,timeRuleId));
        } catch (Exception e) {
            e.printStackTrace();
            return Response.getError(e.getMessage());
        }
    }

}
